package com.mingyizhudao.app.view.mine.adpter;


import android.app.Activity;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.network.Net;
import com.dixiang.framework.utils.AlertDialogs;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.common.BaseViewHolder;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.mine.OrderListCommonActivity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;

public class OrderItemCommonAdpter extends ArrayAdapter<OrderItemEntity>{
	private Activity context;
	LayoutInflater inflater;
	public OrderItemCommonAdpter(Activity context ) {
		super(context, android.R.layout.simple_list_item_1);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		OrderHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.lv_item_order, null);
			holder = new OrderHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (OrderHolder) convertView.getTag();
		}
		holder.populateView(position, getItem(position));
		return convertView;
	}


	class OrderHolder extends BaseViewHolder<OrderItemEntity>{

		private TextView tv_orderid ,tv_orderdoctor ,tv_orderhospital ,tv_patient,tv_operation,tv_tag;

		private ImageView iv_tag;

		public OrderHolder(View view) {
			super(view);
			tv_orderid = (TextView)view.findViewById(R.id.tv_orderid);
			tv_orderdoctor = (TextView)view.findViewById(R.id.tv_orderdoctor);
			tv_orderhospital = (TextView)view.findViewById(R.id.tv_orderhospital);
			tv_patient = (TextView)view.findViewById(R.id.tv_patient);
			iv_tag = (ImageView)view.findViewById(R.id.iv_tag);

			tv_operation  = (TextView)view.findViewById(R.id.tv_operation);
			tv_tag = (TextView)view.findViewById(R.id.tv_tag);
		}

		@Override
		public void populateView(int position, final OrderItemEntity item) {
			tv_orderid.setText(item.getRefNo());
			String doctorName = Utils.isStringEmpty(item.getExpertName()) ? "未填写" : item.getExpertName();
			tv_orderdoctor.setText(doctorName);
			//tv_orderhospital.setText(item.getHpShortName()+"/"+item.getHpShortName());
			if( item.getHpName()==null|| (item.getHpName()!=null&&(item.getHpName().equals( "null" )||item.getHpName().equals( "" ))))
			{
				tv_orderhospital.setText(item.getHpDeptName());//简称
			}else {
				tv_orderhospital.setText(item.getHpName()+"/"+item.getHpDeptName());//简称
			}

			if (item.getContact_name()!=null) {
				tv_patient.setText(item.getContact_name());
			}


			if ("1".equals(item.getBk_status())) {
				iv_tag.setImageResource(R.drawable.state_daifukuan);
				tv_tag.setText("待付款");
				tv_tag.setTextColor(context.getResources().getColor(R.color.fuzhu_brown));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_yellow);
			}

			if ("2".equals(item.getBk_status())) {
				iv_tag.setImageResource(R.drawable.state_anpaizhong);
				tv_tag.setText("安排中");
				tv_tag.setTextColor(context.getResources().getColor(R.color.fuzhu_brown));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_yellow);
			}


			if ("5".equals(item.getBk_status())) {
				iv_tag.setImageResource(R.drawable.state_daiqueren);
				tv_tag.setText("待确认");
				tv_tag.setTextColor(context.getResources().getColor(R.color.fuzhu_brown));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_yellow);
			}

			if ("6".equals(item.getBk_status())) {
				iv_tag.setImageResource(R.drawable.state_yiwancheng);
				tv_tag.setText("待评价");
				tv_tag.setTextColor(context.getResources().getColor(R.color.fuzhu_brown));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_yellow);
			}
			if ("8".equals(item.getBk_status())) {
				tv_tag.setText("已完成");
				tv_tag.setTextColor(context.getResources().getColor(R.color.green_tag));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_green);
			}
			

			if ("9".equals(item.getBk_status())) {
				iv_tag.setImageResource(R.drawable.state_yiquxiao);
				tv_tag.setText("已取消");
				tv_tag.setTextColor(context.getResources().getColor(R.color.green_tag));
				tv_tag.setBackgroundResource(R.drawable.radius_border_bg_green);
			}


			if ("1".equals(item.getBk_status())||"2".equals(item.getBk_status())) {

				tv_operation.setVisibility(View.VISIBLE);
				
				final QJNetUICallback callback = new QJNetUICallback (context.getApplicationContext(),OrderListCommonActivity.class.getSimpleName()) {
					@Override
					public void onSuccess(Object result , String flg) {
						Toast.makeText(context, "操作成功！", 0).show();
						((OrderListCommonActivity)context).doNet();
					}
					
					@Override
					public void onCompleted(Exception e, String result) {
						LoadingDialog.getInstance(context).dismiss();
					}
				};

				tv_operation.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						
						if ("1".equals(item.getBk_status())) {

							AlertDialogs dlg = AlertDialogs.getAlertDialog( context);
							dlg.setCancel("否");
							dlg.setOk( "是" );
							dlg.setTitle("", 13);
							dlg.showAlertDialog(
									"确认取消该订单？",
									20,
									new OnClickListener() {
										@Override
										public void onClick(View arg0) {
											LoadingDialog.getInstance(context).show();
											String url = Constants.baseURL+"booking/cancelBooking/"+item.getId();
//											url = url+ CommonUtils.getTokenParam(context)+"&bk_status="+ item.getBk_status();
//											url = URLHandler.getEncryptUrl(url);
//											CommonUtils.doPut( url, "", mHandler );
											Net.fetch(url, getPutParams(item.getBk_status()), new TypeToken<QjResult<Object>>() {}, callback , Constants.HTTP_INDEX.Fourth);
											
										}
									});
						}

						if ("2".equals(item.getBk_status())) {

							AlertDialogs dlg = AlertDialogs.getAlertDialog( context);
							dlg.setOk( "确认" );
							dlg.setTitle("取消订单？");
							dlg.setContent2(true, "（退款条件请参阅“个人中心-常见问题-关于费用”）" );
							dlg.showAlertDialog(
									"名医助手将尽快与您联系安排退款",
									15,
									new OnClickListener() {
										@Override
										public void onClick(View arg0) {
											LoadingDialog.getInstance(context).show();
											String url = Constants.baseURL+"booking/cancelBooking/"+item.getId();
//											url = url+ CommonUtils.getTokenParam(context)+"&bk_status="+ item.getBk_status();
//											url = URLHandler.getEncryptUrl(url);
//											CommonUtils.doPut( url, "", mHandler );
											Net.fetch(url, getPutParams(item.getBk_status()), new TypeToken<QjResult<Object>>() {}, callback , Constants.HTTP_INDEX.Fourth);
										}
									});
						}

					}
				});

			}
			else {
				tv_operation.setVisibility(View.GONE);
			}
		}
		
	}	
	private StringBuffer getPutParams(String status){
		JsonObject json = new JsonObject();
		json.addProperty("username", CommonUtils.getMobile(context));
		json.addProperty("token", CommonUtils.getToken(context));
		json.addProperty("bk_status", status);
		StringBuffer buffer = new StringBuffer("");
		buffer.append("{\"cancelBooking\":");
		buffer.append(json.toString());
		buffer.append("}");
		return buffer;
	}

}
